package BlockR;

import BlockR.function.*;
import BlockR.model.RandomNumOBJ;
import cpabe.AESCoder;
import it.unisa.dia.gas.jpbc.Element;

import java.io.IOException;

/**
 * @author spencercjh
 */
public class Encrypt {
    public static RandomNumOBJ firstBlockKey = null;

    public static byte[][][] encrypt(String inputfile, Element m) {
        try {
            firstBlockKey = new RandomNumOBJ();
            firstBlockKey.setR1(AESCoder.getRawKey(m.toBytes()));
            firstBlockKey.setR2(RandomNum.generate_test_random_num1(firstBlockKey.getR1().length));
        } catch (Exception e) {
            System.err.println("生成R1R2失败");
            e.printStackTrace();
        }
        int arraySize = firstBlockKey.getR1().length;
        try {
            MappedBiggerFileReader.Read_File(inputfile, arraySize);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("i/o失败");
        }
        byte[][] allPiece = MappedBiggerFileReader.getAll_Piece();
        byte[][][] allBlock = MappedBiggerFileReader.getAll_Block();
        Fill_Final_Piece.Fill(allPiece, allBlock, arraySize);
        try {
            allBlock = Do_Encrypt.DoEncrypt(allPiece, allBlock, GetCipherCode.CipherCode(arraySize), firstBlockKey, arraySize);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("DoEncrypt失败");
        }

        return allBlock;
    }

}
